package com.gopi.utilities.gui;

import java.awt.Component;
import java.awt.GridBagLayout;
import java.awt.TextField;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.text.JTextComponent;

import com.gopi.utilities.action.AbstractController;

public class GuiFactory 
{
	
	public static JPanel createGridBagPanel()
	{
		JPanel panel = new JPanel(new GridBagLayout());
		return panel;
	}
	
	public static JTextField createTextField(String text,int col)
	{
		return new JTextField(text,col);
	}
	
	public static JTextField createUnEditableTextField(String text)
	{
		return createUnEditableTextField(text, text.length());
	}
	
	public static void setText(JComponent comp,String s)
	{
		if(comp instanceof JTextComponent)
		{
			((JTextComponent) comp).setText(s);
		}
	}
	
	public static void setEditable(JComponent comp,boolean s)
	{
		if(comp instanceof JTextComponent)
		{
			((JTextComponent) comp).setEditable(s);
		}
	}
	
	public static JTextField createUnEditableTextField(int len)
	{
		return createUnEditableTextField("", len);
	}
	
	public static JTextField createUnEditableTextField(String text,int col)
	{
		JTextField field = new JTextField(text,col);
		field.setEditable(false);
		return field;
	}
	
	public static JButton createButton(String text,String actionCommand,String iconKey,ActionListener listener)
	{
		return createButton(text,actionCommand,ImageLoader.getInstance().getIcon(iconKey),listener);
	}
	
	public static JButton createButton(String text,String actionCommand,String iconKey,ActionListener listener,AbstractController controller)
	{
		return createButton(text,actionCommand,ImageLoader.getInstance().getIcon(iconKey),listener,controller);
	}
	
	public static JButton createButton(String text,String actionCommand,
			Icon icon,ActionListener listener)
	{
		return createButton(text,actionCommand,icon,listener,null);
	}

	public static JButton createButton(String text,String actionCommand,
			Icon icon,ActionListener listener,AbstractController controller)
	{
		JButton button = new JButton(text,icon);
		button.setActionCommand(actionCommand);
		if(listener!=null)
		{
			button.addActionListener(listener);
		}
		addToController(actionCommand,button,controller);
		return button;
	}
	
	public static JTextField createBorderlessTextField(int length)
	{
		JTextField field = new JTextField(length);
		field.setBorder(BorderFactory.createEmptyBorder());
		
		return field;
	}
	
	public static JTextField createTextField(int length)
	{
		JTextField field = new JTextField(length);
		return field;
	}
	
	private static void addToController(String actionCommand,Component component,AbstractController controller)
	{
		if(controller!=null)
		{
			controller.addActionCommand(actionCommand, component);
		}
	}
}
